<?php
	session_start();
	set_time_limit(0);
	include_once('init.php');
	try {
	$m  = new Mongo();
	} catch(Exception $e) {
	  echo "连接数据库失败:".$e->getMessage();	
	}
	$db = $m->selectDB('foobar');
	$collection = $db->selectCollection("test1");
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>PHP 操作 mongodb 数据库 by 此题无解</title>
		<meta charset="UTF-8">
		<meta name=description content="">
		<meta name=viewport content="width=device-width, initial-scale=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<!-- Bootstrap CSS -->
		<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
		<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
	</head>
	<body>
	<h1 class="text-center">PHP 操作 mongodb 数据库(数组修改器)</h1>
	<div class="container">
		<div class="row">
		1. 插入测试数据
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'inserttest.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$data = array(
				'title'=>'A Aritcle Title',
				'content'=>'...',
				);
			$collection->remove();
			$collection->insert($data);
			$cursor = $collection->find(array('title'=>'A Aritcle Title'));
			$data = $cursor->getNext();
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($data);echo ' <br>运行时间:'.runtime(1);?></pre>
		</div>

		2. $push 使用push添加一条评论,如果push向不存在的键添加数据，键将自动创建
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'push.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$comment = array(
				'name'=>'jon','email'=>'jon@a.cn','content'=>'nice job!',
				);
			$collection->update(array('title'=>'A Aritcle Title'), array('$push'=>array('comment'=>$comment)));
			$cursor = $collection->find(array('title'=>'A Aritcle Title'));
			$result = $cursor->getNext();
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(2);?></pre>
		</div>		

		3. $ne $push 当name不等于abc 向数据中push good=>1
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'nepush.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$collection->update(array('name'=>array('$ne'=>'abc')), array('$push'=>array('good'=>1)));
			$cursor = $collection->find(array('title'=>'A Aritcle Title'));
			$result = $cursor->getNext();
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(3);?></pre>
		</div>	

		4. $ne $push 当title不等于A Aritcle Title 向数据中push bad=>1
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'nepush1.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$collection->update(array('title'=>array('$ne'=>'A Aritcle Title')), array('$push'=>array('bad'=>1)));
			$cursor = $collection->find(array('title'=>'A Aritcle Title'));
			$result = $cursor->getNext();
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(4);?></pre>
		</div>	

		5. $addtoset 使用addtoset向文章中添加一个邮箱
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'addtoset.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$collection->update(array('title'=>'A Aritcle Title'), array('$addToSet'=>array('mails'=>'1@1.cn')));
			$cursor = $collection->find(array('title'=>'A Aritcle Title'));
			$result = $cursor->getNext();
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(5);?></pre>
		</div>	

		6. $addToSet $each 使用addtoset向文章中添加二个邮箱
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'addtoseteach.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$collection->update(array('title'=>'A Aritcle Title'), array('$addToSet'=>array('mails'=>array('$each'=>array('2@2.cn','3@3.cn')))));
			$cursor = $collection->find(array('title'=>'A Aritcle Title'));
			$result = $cursor->getNext();
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(6);?></pre>
		</div>	
	
		7. $pop 删除邮件,key=1删除数组末元素
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'pop.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$collection->update(array('title'=>'A Aritcle Title'), array('$pop'=>array('mails'=>1)));
			$cursor = $collection->find(array('title'=>'A Aritcle Title'));
			$result = $cursor->getNext();
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(7);?></pre>
		</div>

		8. $pop 删除邮件,key=-1删除数组头元素
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'pop1.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$collection->update(array('title'=>'A Aritcle Title'), array('$pop'=>array('mails'=>-1)));
			$cursor = $collection->find(array('title'=>'A Aritcle Title'));
			$result = $cursor->getNext();
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(8);?></pre>
		</div>

		9. $定位符，匹配查询中已经匹配过的元素，name已经查找过，修改时直接修改了jon
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'search1.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$collection->update(array('comment.name'=>'jon'), array('$set'=>array('comment.$.name'=>'jim')));
			$cursor = $collection->find(array('title'=>'A Aritcle Title'));
			$result = $cursor->getNext();
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(9);?></pre>
		</div>

		10. upsert 如果没有文档匹配到就，以这个匹配条件和更新文档创建一个文档
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'upsert.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$collection->update(
				array('url'=>'/blog'), 
				array('$inc'=>array('vist'=>1)),
				array('upsert'=>true)
				);
			$cursor = $collection->find(array('url'=>'/blog'));
			$result = $cursor->getNext();
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);echo ' <br>运行时间:'.runtime(10);?></pre>
		</div>

		

		<div><blockquote><em>php</em>操作<em>mongodb</em>数据库入门篇(3)<br>
		<a href="fourth.php">下一篇</a>
		<small class="pull-right"><cite>此题无解</cite></small></blockquote></div>

		</div>
	</div>

		<!-- jQuery -->
		<script src="./js/jquery.min.js"></script>
		<!-- Bootstrap JavaScript -->
		<script src="./js/bootstrap.min.js"></script>
		<!-- prettify -->
		<script src="./js/prettify.js"></script>
		<script>
		$(function(){
			prettyPrint();
		});
		</script>
	</body>
</html>